<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace App\Views\Admin;

use Sun\Sun;
use Sun\Html;
use Sun\Utils;
use App\Models;

class User extends Main {

    public function __construct() {
        parent::__construct();
        $this->data_scene = 'UserList';
    }

    public function UserListSwitch() {
        $page_switch = new Html\PageSwitch("UserList");
        return $page_switch->ToHtml();
    }

    public function ListPageScene() {
        $users = new Models\User();

        // фильтр
        $field = @$this->data['FieldType'];
        $val = @$this->data['FieldValue'];
        $type = @$this->data['UserType'];

        $cond = $type == null || $type == -1 ? "true" : "\"user\".type_id = ".$type;
        $cond .= empty($val) ? "" : " and lower({$field}::VARCHAR) LIKE lower({$users->db->quote($val)})";

        $num_rows = Sun::conf("iface/usr_rows");
        $num_pages = ceil($users->UserCount($cond) / $num_rows);

        $p = $this->data['Page'];
        $offset = $num_rows * ($p - 1);

        $res = $users->UserList($offset, $num_rows, $cond);

        $table = new Html\QueryTable($res,
            array('Ид', 'Тип', 'Логин', 'ФИО', 'Дата регистрации', 'Дата окончания'));
        $table->id = 'ListUserTable';

        $rows = $table->First()->Childs();
        $rows[0]->AddData()->Added()->SetAttribute('class', 'ControlCell');

        for ($i = 1; $i < count($rows); ++$i) {
            $id = $rows[$i]->First()->First()->value;
            $rows[$i]->SetAttribute("data-id", $id);
            $rows[$i]->AddData()->Added()
                ->Add(new Html\A(array('href' => $id)))->Added()
                    ->Add(new Html\Img(array(
                        'src' => 'pub/media/images/user_info_16.png',
                        'alt' =>'Edit')));
        }

        ////////////////// javascript //////////////////////
        $src = Utils::LoadFileToString(dirname(__FILE__)."/User/UserList.js");
        $script = new Html\Script($src);
        ////////////////////////////////////////////////////

        $div = new Html\Div();
        $div->add($table);
        $div->Add($script);

        $query_res = "{ content: {$div->ToJSON()}, num_pages: {$num_pages} }";
        echo $query_res;
    }


    // изменение
    public function AltScene() {
        if (empty($this->cond)) {
            $user = new Models\User();
            $profile = $user->UserInfo($this->data['user_id']);
            $this->data = array_merge($this->data, $profile);
        }
        $this->IncludeScene("AltBody");
    }

    // поиск
    public function FieldType() {
        $select = new Html\Form\Select(array(
            'id' => "FieldType"
        ));
        $types = array(
            'user_id' => "Ид",
            'fullname' => "ФИО",
            'login' => "Логин"
        );
        foreach ($types as $v => $text) {
            $select->AddOption($v, $text);
        }
        return $select;
    }

    public function UserTypes($id, $all = true) {
        $user = new Models\User();

        $select = new Html\Form\Select();
        $select->id = $id;
        $types = $user->UserTypes();

        if ($all) $select->AddOption(-1, "Все", array(), -1);
        foreach ($types as $type) {
            $select->AddOption($type[0], $type[1], array(), $type[0]);
        }

        if (isset($this->data[$id]))
            $select->Get($this->data[$id])->selected = "selected";
        return $select;
    }


    public function NewLogin() {
        $user = new Models\User();
        return 'user'.$user->GetCurrentId();
    }

    public function NewPass() {
        return Utils::RandomString(6, '0123456789');
    }

}
